package org.ahuanlien.crm.service.impl;

import org.ahuanlien.crm.domain.*;
import org.ahuanlien.crm.mapper.ToBeAuditedBillMapper;
import org.ahuanlien.crm.query.ToBeAuditedBillQueryObbject;
import org.ahuanlien.crm.service.*;
import org.ahuanlien.crm.util.LogicException;
import org.ahuanlien.crm.util.PageResult;
import org.ahuanlien.crm.util.SnGeneratorUtil;
import org.ahuanlien.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ToBeAuditedBillIServiceImpl implements IToBeAuditedBillIService {

    @Autowired
    ToBeAuditedBillMapper toBeAuditedBillMapper;
    @Autowired
    ICaseReportBillService caseReportBillService;
    @Autowired
    ICaseReportBillItemService caseReportBillItemService;
    @Autowired
    IBillRecordService billRecordService;
    @Autowired
    IClientRepaymentInfoService clientRepaymentInfoService;

    public void submitToBeAudited(String caseNumber) {
        ToBeAuditedBill toBeAuditedBill = new ToBeAuditedBill();
        /*使用传入的报案单据, 查询出来各个属性的值, 设置给待审核单据*/
        CaseReportBill caseReportBill = caseReportBillService.getByCaseNumber(caseNumber);
        /*在后台检查当前的报案单的状态, 如果不是已受理, 那么爆出异常*/
        if (caseReportBill.getStatus() != 1) {
            throw new LogicException("当前报案单不是已受理状态, 不可以提交待审查");
        }

        CaseReportBillItem caseReportBillItem = caseReportBillItemService.getByCaseNumber(caseNumber);

        /*生成一张理赔申请单据*/
        toBeAuditedBill.setApplyNumber(SnGeneratorUtil.getSn("app"));
        toBeAuditedBill.setCaseHandlerName(caseReportBillItem.getCaseHandler().getUsername());
        toBeAuditedBill.setCaseNumber(caseReportBill.getCaseNumber());
        toBeAuditedBill.setClaimerAccount(caseReportBillItem.getClaimerAccount());
        toBeAuditedBill.setClaimerName(caseReportBillItem.getClaimerName());
        toBeAuditedBill.setInputUserName(UserContext.getCurrentUser().getUsername());
        toBeAuditedBill.setInsuranceBillNumber(caseReportBill.getInsuranceBillNumber());
        toBeAuditedBill.setLostClaimAmount(caseReportBillItem.getLostClaimAmount());
        Employee receptionEmployee = caseReportBill.getReceptionEmployee();
        String username = receptionEmployee.getUsername();
        toBeAuditedBill.setReceptionName(username);
        toBeAuditedBill.setStatus(toBeAuditedBill.NORMAL);
        toBeAuditedBill.setSubmitDate(new Date());

        /*插入新的待审核单据*/
        toBeAuditedBillMapper.insert(toBeAuditedBill);
        /*改变报案单的状态为待审核*/
        caseReportBillService.changeStatus(caseNumber, CaseReportBill.TOBEAUDITED);

    }

    /*审核理赔申请单的方法*/
    public void audit(String caseNumber) {
        /*查询当前理赔申请单的状态, 是否为待审核 , 如果不是 , 抛出自定义异常*/
        ToBeAuditedBill toBeAuditedBill = toBeAuditedBillMapper.getByCaseNumber(caseNumber);
        if (toBeAuditedBill.getStatus() != 0) {
            throw new LogicException("当前审核单据已退回或者已审核");
        }
        /*改变当前申请单的状态 , 改变报案单的状态 , 添加审核人属性, 添加审核日期属性*/
        CaseReportBill caseReportBill = caseReportBillService.getByCaseNumber(caseNumber);
        caseReportBillService.changeStatus(caseNumber, CaseReportBill.AUDITED);
        toBeAuditedBill.setStatus(ToBeAuditedBill.AUDITED);
        toBeAuditedBill.setAuditDate(new Date());
        toBeAuditedBill.setAuditor(UserContext.getCurrentUser());
        toBeAuditedBillMapper.audit(toBeAuditedBill);

        /*在审核申请单之后,自动生成一张表*/
        ClientRepaymentInfo repaymentInfo = new ClientRepaymentInfo();
        repaymentInfo.setRepaymentInfoNumber(SnGeneratorUtil.getSn("repay"));
        repaymentInfo.setCaseNumber(caseNumber);
        String insuranceBillNumber = caseReportBill.getInsuranceBillNumber();
        repaymentInfo.setInsuranceBillNumber(insuranceBillNumber);
        BillRecord billRecord = billRecordService.getBySn(insuranceBillNumber);
        Client client = billRecord.getHolder();
        repaymentInfo.setClient(client);
        repaymentInfo.setCompanyName("平安保险公司");
        repaymentInfo.setHandler(UserContext.getCurrentUser());
        repaymentInfo.setStatus(ClientRepaymentInfo.UNINFORMED);
        clientRepaymentInfoService.insert(repaymentInfo);
    }

    /*退回赔偿申请单的方法*/
    public void reject(String caseNumber) {
        /*查询当前理赔申请单的状态, 是否为待审核 , 如果不是 , 抛出自定义异常*/
        ToBeAuditedBill toBeAuditedBill = toBeAuditedBillMapper.getByCaseNumber(caseNumber);
        if (toBeAuditedBill.getStatus() != 0) {
            throw new LogicException("当前审核单据已退回或者已审核");
        }
        /*改变当前申请单的状态 , 改变报案单的状态 */
        CaseReportBill caseReportBill = caseReportBillService.getByCaseNumber(caseNumber);
        caseReportBillService.changeStatus(caseNumber, CaseReportBill.ACCEPTED);
        toBeAuditedBill.setStatus(ToBeAuditedBill.REJECTED);
        /*设置与退回相关的信息*/
        toBeAuditedBill.setRejectDate(new Date());
        toBeAuditedBill.setRejector(UserContext.getCurrentUser());
        toBeAuditedBillMapper.reject(toBeAuditedBill);

    }

    public PageResult query(ToBeAuditedBillQueryObbject qo) {
        int count = toBeAuditedBillMapper.queryForCount(qo);
        if (count == 0) {
            return new PageResult(count, Collections.EMPTY_LIST);
        }
        List<CaseReportBill> result = toBeAuditedBillMapper.queryForList(qo);
        return new PageResult(count, result);
    }
}
